<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17
 * Time: 17:33
 */

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\EbetTrait;
use Illuminate\Support\Facades\Cache;

class ebet extends Game
{
    use EbetTrait;

    private $platName = 'ebet';



    const API_PLAYER_CREATE = '/api/syncuser';
    const API_PLAYER_BALANCE = '/api/getusermoney';
    const API_TRANSFER_IN = '/api/recharge';
    const API_TRANSFER_OUT = '/api/recharge';
    const API_CHECK_ORDER_STATUS = '/api/rechargestatus';
    const API_BET_RECORD = '/api/userbethistory';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'live',
            'status' => 1
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array|mixed
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {

        $url = $this->gameUrl . self::API_PLAYER_CREATE;
        $data = [
            'username' => $accountUserName,
            'channelId' => $this->channelId,
            'lang' => 1,
            'timestamp' => Carbon::now()->timestamp,
            'signature' => $this->getSign($accountUserName),
            'subChannelId' => 0
        ];

        $result = $this->request('POST', $url, $data, $this->headers);
        $result = json_decode($result, true);

        if (array_key_exists('status', $result)) {
            if ($result['status'] == 200) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，返回码：' . $result['status']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {
        $url = $this->gameUrl . self::API_PLAYER_BALANCE;
        $data = [
            'username' => $accountUserName,
            'channelId' => $this->channelId,
            'signature' => $this->getSign($accountUserName)
        ];

        $this->headers = [
            'Content-type' => 'application/x-www-form-urlencoded'
        ];

        $result = $this->request('POST', $url, $data, $this->headers);
        $result = json_decode($result, true);

        if (array_key_exists('status', $result)) {
            if ($result['status'] == 200) {
                return ['success' => true, 'data' => ['balance' => $result['results'][0]['wallet'][0]['money'],], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，返回码：' . $result['status']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_IN;
        $timestamp = time();
        $data = [
            'username' => $accountUserName,
            'channelId' => $this->channelId,
            'money' => $price,
            'rechargeReqId' => $transferid,
            'signature' => $this->getSign($accountUserName . $timestamp),
            'timestamp' => time(),
            'typeId' => 0
        ];

        $result = $this->request('POST', $url, $data, $this->headers);
        $result = json_decode($result, true);

        if (array_key_exists('status', $result)) {
            if ($result['status'] == 200) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错误码：' . $result['status']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }


    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_OUT;
        $timestamp = time();
        $data = [
            'username' => $accountUserName,
            'channelId' => $this->channelId,
            'money' => 0 - floatval($price),
            'rechargeReqId' => $transferid,
            'signature' => $this->getSign($accountUserName . $timestamp),
            'timestamp' => time(),
            'typeId' => 0
        ];

        $result = $this->request('POST', $url, $data, $this->headers);
        $result = json_decode($result, true);

        if (array_key_exists('status', $result)) {
            if ($result['status'] == 200) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错误码：' . $result['status']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        $url = $this->gameUrl . self::API_CHECK_ORDER_STATUS;
        $data = [
            'channelId' => $this->channelId,
            'rechargeReqId' => $request->transferid,
            'signature' => $this->getSign($request->transferid),
        ];

        $result = $this->request('POST', $url, $data, $this->headers);
        $result = json_decode($result, true);

        if (array_key_exists('status', $result)) {
            if ($result['status'] == 200) {
                return ['success' => true, 'data' => ['transfer_state'=>1], 'message' => ''];
            } elseif($result['status'] == 5001) {
                return ['success' => false, 'data' => ['transfer_state'=>0], 'message' => '查询失败，失败错误码：' . $result['status']];
            } else{
                return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '查询失败，失败错误码：' . $result['status']];
            }

        } else {
            return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '请求平台失败，状态未知'];
        }
    }

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有此功能'];
    }

    /**
     * gameapi
     * 2018/11/29 14:26
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinGame($request)
    {
        $params = [
            'plat' => 'ebet',
            'site_username' => $request->username,
            'username' => $request->accountUserName,
            'password' => $request->password,
        ];
        $token = encrypt(json_encode($params));
        cache([strtolower($request->accountUserName) => $token], Carbon::now()->addSeconds(60));
        $token = str_replace('=', '-', $token);
        if ($token) {
            return ['success' => true, 'data' => ['url' => $this->joinGame . '?username=' . $request->accountUserName . '&accessToken=' . $token], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '游戏登录失败，登录token未获取到'];
        }

    }

    /**
     * gameapi
     * 2018/12/11 14:17
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }

    /**
     * gameapi
     * 2018/11/22 19:22
     * Administrator
     * curlGetBetRecord
     * 获取记录
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $url = $this->gameUrl . self::API_BET_RECORD;
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();
        $time = time();
        $page = 1;


        do {
            $flag = false;
            $params = [
                'channelId' => $this->channelId,
                'subChannelId' => 0,
                'betStatus' => 1,
                'timestamp' => $time,
                'pageNum' => $page,
                'pageSize' => 5000,
                'signature' => $this->getSign($time),
                'startTimeStr' => $start_time,
                'endTimeStr' => $end_time,
            ];

            $result = $this->request('POST', $url, $params, $this->headers);
            $result = json_decode($result, true);
            if (is_array($result)) {
                if (array_key_exists('status', $result)) {
                    if ($result['status'] == 200) {
                        $betRecords = $result['betHistories'];
                        if (count($betRecords) >= 1) {
                            $data = array_merge($data, $betRecords);
                            $flag = true;
                        }
                    }
                }
            }
            $page++;
        } while ($flag);

        return $data;
    }
}